package com.hikari.leetcode.day01;

import java.util.HashMap;
import java.util.Map;

public class Test01 {
    public static void main(String[] args) {
        String [] str = new String[] {"flower","flow","flight"};

        System.out.println(addBinary("10","01"));

    }

    //整数反转
    public static int reverse(int x){
        long a = 0;
        while (x != 0){
            a = a*10 + x%10;
            x = x/10;
        }
        return (int)a == a? (int) a:0;
    }

    //================================================================================
    //回文数
    public static boolean isPalindrome(int x) {
        if (x<0) return false;
        int reverse = reverse(x);
        System.out.println("x = "+ x);
        return x==reverse;
    }
//================================================================================
    //罗马数字转整数
    public static int romanToInt(String x){
        int a = 0;
         int first = getValue(x.charAt(0));
        for (int i = 1; i < x.length(); i++) {
            Integer next = getValue(x.charAt(i));
            if (next>first){//如果右边大于左边
                a-=first;
            }else {
                a+=first;
            }
            first = next;
        }
        a += first;
        return a;
    }
    private static int getValue(char ch) {
        switch (ch) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }
    }
//================================================================================
    //最长公共前缀
    public static String longestCommonPrefix(String[] strs) {
        if(strs.length==0)return "";
        //公共前缀比所有字符串都短，随便选一个先
        String s=strs[0];
        for (String string : strs) {
            while(!string.startsWith(s)){
                if(s.length()==0)return "";
                //公共前缀不匹配就让它变短！
                s=s.substring(0,s.length()-1);
            }
        }
        return s;
    }

    //求两个整数的商 要求不能用 / * %
    public static int divide(int a, int b) {
        if(a==-2147483648&&b==-1)
            return 2147483647;
        if(a<0&&b<0)
            return Integer.divideUnsigned(-a, -b);
        if(a<0&&b>0)
            return -(Integer.divideUnsigned(-a, b));
        if(a>0&&b<0)
            return -(Integer.divideUnsigned(a, -b));
        return Integer.divideUnsigned(a, b);
    }


    //二进制计算
    public static String addBinary(String num1, String num2) {
        StringBuilder sb = new StringBuilder();
        int carry = 0, i = num1.length()-1, j = num2.length()-1;

        while(i >= 0 || j >= 0 || carry != 0){
            int res = carry;
            if(i>=0) {
                res += num1.charAt(i)-'0';//‘3’-‘0’=3， char 类型可以做+ —
                i--;
            }
            if(j>=0) res += num2.charAt(j--)-'0';//同上代码
            sb.append(res%2);//小于10 ，模是本身，大于10 ，模是余数
            carry = res/2; // 大于10 ，进位1，否则为0
        }
        return sb.reverse().toString();// sb是一个容器，最后要转为字符串，to string

    }
}
